<?php 
class PlaceController extends Zend_Controller_Action{
	// Mảng tham số nhận được ở mỗi Action:
	protected  $_arrParam;
	private $userId;
	private $NO_IMAGE = "no_image.gif";
	private $URI = "/";
	private $NON_DISPLAY = 0;

	public function init(){
		$this->_arrParam = $this->_request->getParams();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;

		$mAccount = new Default_Model_Account();
		$this->URI = $this->getRequest()->getScheme()."://".$this->getRequest()->getHttpHost().$this->getRequest()->getRequestUri();
		if(!$mAccount->hasIdentity()) {
			$this->_redirect('/account/login/?returnurl='.$this->URI);
		} else {
			$this->NON_DISPLAY = 1;
		}

		$this->userId = $mAccount->getId();

		$this->view->nickname = $mAccount->getNickName();
		$this->view->returnurl = $this->URI;
		$this->view->nondisplay = $this->NON_DISPLAY;
		$this->view->userid = $mAccount->getId();
	}

	public function indexAction() {

	}

	public function viewAction() {
		$mPlace = new Default_Model_Place();
		$mImage = new Default_Model_Image();

		// list place of user
		$arrListPlace = $mPlace->listNewestPlace();
		$this->view->arrListPlace = $arrListPlace;
		if(isset($this->_arrParam['id'])) {
			$arr = $mPlace->viewPlaceById($this->_arrParam['id']);
			if(count($arr) == 0) {
				$this->_redirect('/place/add/');
			} else {
				$this->view->id = $arr[0]['Id'];
				$this->view->locname = $arr[0]['Name'];
				$this->view->longval = $arr[0]['Longitude'];
				$this->view->latval = $arr[0]['Latitude'];
				if($this->userId == $arr[0]['UserId']) {
					$this->view->displaybtnedit = 1;
				} else {
					$this->view->displaybtnedit = 0;
				}
				if($arr[0]['Photo'] == ""){
					$this->view->photo = $mImage->getImgThumbPath().$this->NO_IMAGE;
				} else {
					$this->view->photo = $mImage->getImgThumbPath().$arr[0]['Photo'];
				}
				$this->view->description = $arr[0]['Description'];
			}
		} else {
			$referrerUrl = VnSpot_Utility_Page::safelyGetReferrerUrl('/');
			if($referrerUrl == '/') {
				$this->_redirect("/place/add/");
			} else {
				$this->_redirect($referrerUrl);
			}
		}
	}

	public function addAction() {
		if($this->_request->isPost()){
			$arrParam = null;
			if(!empty($_FILES['picture']['name'])) {
				try {
					$mImage = new Default_Model_Image();
					if($mImage->uploadImg()) {
						$mImage->createThumbImg();
						$arrParam = array(
								'Name' => $this->_arrParam['locnamereal'],
								'UserId' => $this->userId,
								'Description' => $this->_arrParam['description'],
								'Longitude' => $this->_arrParam['longvalreal'],
								'Latitude' => $this->_arrParam['latvalreal'],
								'AddedTime' => date('Y-m-d H:i:s'),
								'Photo' => $mImage->getImgName()
						);
					} else {
						echo $mImage->getError();
					}
				} catch(Exception $e) {
					echo $e->getMessage();
				}
			} else {
				$arrParam = array(
						'Name' => $this->_arrParam['locnamereal'],
						'UserId' => $this->userId,
						'Description' => $this->_arrParam['description'],
						'Longitude' => $this->_arrParam['longvalreal'],
						'Latitude' => $this->_arrParam['latvalreal'],
						'AddedTime' => date('Y-m-d H:i:s')
				);
			}
			$mPlace = new Default_Model_Place();
			$mPlace->insertPlace($arrParam);
			$this->_redirect("/place/edit/");
		}
	}
	public function addexAction() {
		$referrerUrl = VnSpot_Utility_Page::safelyGetReferrerUrl('/');
		if($referrerUrl == '/') {
			$this->_redirect("/place/add/");
		} else {
			if($this->_request->isPost()){
				$arrParam = null;
				if(!empty($_FILES['picture']['name'])) {
					try {
						$mImage = new Default_Model_Image();
						if($mImage->uploadImg()) {
							$mImage->createThumbImg();
							$arrParam = array(
									'Name' => $_POST['locname'],
									'UserId' => $this->userId,
									'Description' => $_POST['description'],
									'Longitude' => $_POST['longval'],
									'Latitude' => $_POST['latval'],
									'AddedTime' => date('Y-m-d H:i:s'),
									'Photo' => $mImage->getImgName()
							);
						} else {
							echo $mImage->getError();
						}
					} catch(Exception $e) {
						echo $e->getMessage();
					}
				} else {
					$arrParam = array(
							'Name' => $_POST['locname'],
							'UserId' => $this->userId,
							'Description' => $_POST['description'],
							'Longitude' => $_POST['longval'],
							'Latitude' => $_POST['latval'],
							'AddedTime' => date('Y-m-d H:i:s')
					);
				}
				$mPlace = new Default_Model_Place();
				$mPlace->insertPlace($arrParam);
				$this->_redirect("/place/edit/");
			}
		}
	}
	public function editAction() {
		$mPlace = new Default_Model_Place();
		$mImage = new Default_Model_Image();

		// list place of user
		$arrListPlace = $mPlace->listPlaceByUserId($this->userId);
		$this->view->arrListPlace = $arrListPlace;

		if(!$this->_request->isPost()) {
			if(isset($this->_arrParam['id'])) {
				$arr = $mPlace->selectPlaceById($this->userId,$this->_arrParam['id']);
				if(count($arr) == 0) {
					$this->_redirect('/place/view/id/'.$this->_arrParam['id']);
				} else {
					$this->view->id = $arr[0]['Id'];
					$this->view->locname = $arr[0]['Name'];
					$this->view->longval = $arr[0]['Longitude'];
					$this->view->latval = $arr[0]['Latitude'];
					if($arr[0]['Photo'] == ""){
						$this->view->photo = $mImage->getImgThumbPath().$this->NO_IMAGE;
					} else {
						$this->view->photo = $mImage->getImgThumbPath().$arr[0]['Photo'];
					}
					$this->view->description = $arr[0]['Description'];
				}
			} else {
				$arr = $mPlace->selectLastPlace($this->userId);
				if(count($arr) == 0) {
					$this->_redirect('/place/view/id/'.$this->_arrParam['id']);
				} else {
					$this->view->id = $arr[0]['Id'];
					$this->view->locname = $arr[0]['Name'];
					$this->view->longval = $arr[0]['Longitude'];
					$this->view->latval = $arr[0]['Latitude'];
					if($arr[0]['Photo'] == ""){
						$this->view->photo = $mImage->getImgThumbPath().$this->NO_IMAGE;
					} else {
						$this->view->photo = $mImage->getImgThumbPath().$arr[0]['Photo'];
					}
					$this->view->description = $arr[0]['Description'];
				}
			}
		} else {
			if(isset($_POST['btnEdit'])) {
				$arrPhoto = $mPlace->getPlacePhoto($this->userId, $this->_arrParam['placeId']);
				try {
					$arrParam = null;
					if(!empty($_FILES['picture']['name'])) {
						if($mImage->uploadImg()) {
							$mImage->createThumbImg();
							// process to delete img + thumb
							if($mImage -> deleteImg($arrPhoto[0]['Photo'])) {
								$arrParam = array(
										'Name' => $this->_arrParam['locname'],
										'Description' => $this->_arrParam['description'],
										'Longitude' => $this->_arrParam['longval'],
										'Latitude' => $this->_arrParam['latval'],
										'EditedTime' => date('Y-m-d H:i:s'),
										'Photo' => $mImage->getImgName()
								);
							}
						} else {
							echo $mImage->getError();
						}

					} else {
						$arrParam = array(
								'Name' => $this->_arrParam['locname'],
								'Description' => $this->_arrParam['description'],
								'Longitude' => $this->_arrParam['longval'],
								'Latitude' => $this->_arrParam['latval'],
								'EditedTime' => date('Y-m-d H:i:s')
						);
					}
					$mPlace->editPlace($arrParam,$this->_arrParam['placeId']);
					$this->_redirect("/place/edit/id/".$this->_arrParam['placeId']);
				} catch(Exception $e) {
					echo $e->getMessage();
				}
			} else if(isset($_POST['btnDelete'])) {
				// delete image
				$arrPhoto = $mPlace->getPlacePhoto($this->userId, $this->_arrParam['placeId']);
				if($mImage -> deleteImg($arrPhoto[0]['Photo'])) {
					// delete place
					$mPlace->deletePlaceById($this->_arrParam['placeId']);
				}
				$this->_redirect("/place/edit/");
			}
		}
	}
}